package com.pro.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ClassName:OrderNoUtil
 * Package:com.pro.utils
 * Description:
 *
 * @Date:2021/11/12 16:31
 * @Author: XJR
 */
public class OrderNoUtil {
    //使用单例模式，不允许直接创建实例
    private OrderNoUtil(){}

    //创建一个空实例对象，类需要用的时候才赋值
    private static OrderNoUtil order = null;

    //单例模式--懒汉模式
    public static synchronized OrderNoUtil getInstance(){
        if(order == null){
            order = new OrderNoUtil();
        }
        return order;
    };

    //全局自增数
    private static int count = 0;

    //每毫秒最多生成多少订单(最好像是9999这种准备进位的值)
    private static final int total = 9999;

    //格式化的时间字符串
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    //获取当前时间年月日时分秒毫秒字符串
    private static String getNoewDatestr(){
        return sdf.format(new Date());
    }
    //记录上一次的时间，用来判断是否需要递增全局书
    private  static String now = null;

    /*生成一个订单号*/
    public synchronized String GenerateOrder(){
        String datastr = getNoewDatestr();
        if(datastr.equals(now)){
            count++;    //自增
        }else{
            count = 1;
            now = datastr;
        }
        int countInteger = String.valueOf(total).length()-String.valueOf(count).length();
        String bu = ""; //补字段
        for (int i = 0; i < countInteger; i++) {
            bu += "0";
        }
        bu += String.valueOf(count);
        if(count >= total){
            count = 0;
        }
        return datastr + bu;
    }
}
